﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Excel;

namespace EWMSWeb.Query
{
    public partial class QueryByBarCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bindData()
        {
            string barcode = txtBarCode.Text;
            rept.DataSource = GetDT(barcode);
            rept.DataBind();
        }

        private DataTable GetDT(string barcode)
        {

            string txtid = txtID.Value;
           
            DataLib.BLL.BarCode bll = new DataLib.BLL.BarCode();
            System.Data.DataTable dt = new System.Data.DataTable();
            string barid = "";
            dt = bll.GetDataList(barcode, txtid, out barid);
            if (barcode != null)
            {
                txtID.Value = barid;
                txtBarCode.Text = "";
            }
            return dt;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            bindData();
        }

        public string GetUrl(Object obj)
        {
            if (obj != null)
                return "";
            else
                return "/Storage/outStorageUpdate.aspx?outid=" + obj.ToString();
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            //
            rept.DataSource = null;
            rept.DataBind();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {


            System.Data.DataTable dt = GetDT(null);
           
            DataTable[] exportData = {dt};

            //只导出第一张表，数据量超大会自动分页。
            string tempFilePath = Server.MapPath("/upload/");
            string fileName = "条码查询_" + DateTime.Now.Ticks.ToString();
            string excelVersion = "";
            string ContentType = "";
            //判断Excel版本

            excelVersion = ExcelVersion.Excel12;
            fileName = fileName + ".xlsx";
            ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
          
            //关键语句：调用ExcelFile组件执行导出操作。
            ExcelFile.SetData(exportData[0], tempFilePath + fileName, excelVersion, HDRType.Yes);

         
            //写文件到客户端
            Session["fileName"] = fileName;
            Session["filePath"] = tempFilePath + fileName;
            Session["ContentType"] = ContentType;
            //防止页面不刷新，从客户端调用下载文件。
            string urlDownload = Request.Url.OriginalString.Replace("QueryByBarCode.aspx", "Excel.aspx");
            urlDownload = urlDownload.Replace("QueryByBarCode.aspx", "Excel.aspx");

            ClientScript.RegisterStartupScript(this.GetType(), "clientScipt",
                "window.open('" + urlDownload + "','','width=1,height=1,toolbar=no,location=no,menubar=no');", true);
        }


    }
}